package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.DesFragmentAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.fragment.sickness.ExpertAdviceFragment;
import com.meihua.baoleyi.fragment.sickness.SickDescriptFragment;
import com.meihua.baoleyi.fragment.sickness.TherapCureFragment1;
import com.meihua.baoleyi.fragment.sickness.TherapFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**诊断结果
 * Created by liming on 2017/10/19.
 */

public class MedicalResultActivity  extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.text_title)
    TextView text_title;//标题
    @Bind(R.id.text_back)
    ImageView text_back;//返回
    @Bind(R.id.tabs)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> mFragment = new ArrayList<>();
    private DesFragmentAdapter mAdapter;
    private TherapCureFragment1 fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medical_result);
        ButterKnife.bind(this);
        init();
    }
    private void init() {
        Intent intent = getIntent();
        String age = intent.getStringExtra("age");
        String sex = intent.getStringExtra("sex");
        String cycle = intent.getStringExtra("cycle");
        String symptoms = intent.getStringExtra("symptoms");
        String parent = intent.getStringExtra("parent");
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("age", age);
        treeMap.put("sex", sex);
        treeMap.put("cycle", cycle);
        treeMap.put("symptoms", symptoms);
        treeMap.put("parent", parent);

        text_title.setText("诊断结果");
        List<String> type = new ArrayList<>();
        type.add("治疗方案");
        type.add("症状综合描述");
        type.add("专家建议");
        mFragment.add(TherapFragment.newInstance(treeMap, 3));
        mFragment.add(SickDescriptFragment.newInstance(treeMap,1));
        mFragment.add(ExpertAdviceFragment.newInstance(treeMap,2));
        mAdapter = new DesFragmentAdapter(getSupportFragmentManager(), type, mFragment);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(viewPager);
        text_back.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text_back://返回
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        fragment = (TherapCureFragment1) getSupportFragmentManager().findFragmentByTag("TherapCureFragment1");
        if (fragment != null) {
            if (null == fragment.getWebview()) return false;
            boolean visibleHint = fragment.getUserVisibleHint();
            if (keyCode == KeyEvent.KEYCODE_BACK && fragment.getWebview().canGoBack() && visibleHint) {
                fragment.getWebview().goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}

